import 'package:encrypt/encrypt.dart';
import 'package:flutter/services.dart';

/**
 * Creat For 11643
 * CreatTime 2022/12/19
 * Description:
 */
class RSATools  {
// 加密 Future代表异步操作
  Future<String> encodeString(String content,String PublicKey) async {
    print("$PublicKey");
    dynamic publicKey = RSAKeyParser().parse(PublicKey);
    final encrypter = Encrypter(RSA(publicKey: publicKey));
    return encrypter.encrypt(content).base64; //返回加密后的base64格式文件
  }
// 解密
  Future<String> decodeString(String content) async {
    final privatePem = await rootBundle.loadString('key/private.pem');
    dynamic privateKey = RSAKeyParser().parse(privatePem);

    final encrypter = Encrypter(RSA(privateKey: privateKey));
    return encrypter.decrypt(Encrypted.fromBase64(content));
  }
}
